
<template>
  <div>
    <div class="gva-form-box">
      <el-form :model="formData" ref="elFormRef" label-position="right" :rules="rule" label-width="80px">
        <el-form-item label="电子发票ID:" prop="eIid">
          <el-input v-model="formData.eIid" :clearable="true"  placeholder="请输入电子发票ID" />
       </el-form-item>
        <el-form-item label="发票类型:" prop="invoiceType">
          <el-input v-model="formData.invoiceType" :clearable="true"  placeholder="请输入发票类型" />
       </el-form-item>
        <el-form-item label="增值税发票类型:" prop="generalOrSpecialVat">
          <el-input v-model="formData.generalOrSpecialVat" :clearable="true"  placeholder="请输入增值税发票类型" />
       </el-form-item>
        <el-form-item label="纳税人类型:" prop="taxpayerType">
          <el-input v-model="formData.taxpayerType" :clearable="true"  placeholder="请输入纳税人类型" />
       </el-form-item>
        <el-form-item label="销售方税号:" prop="sellerIdNum">
          <el-input v-model="formData.sellerIdNum" :clearable="true"  placeholder="请输入销售方税号" />
       </el-form-item>
        <el-form-item label="销售方名称:" prop="sellerName">
          <el-input v-model="formData.sellerName" :clearable="true"  placeholder="请输入销售方名称" />
       </el-form-item>
        <el-form-item label="销售方地址:" prop="sellerAddr">
          <el-input v-model="formData.sellerAddr" :clearable="true"  placeholder="请输入销售方地址" />
       </el-form-item>
        <el-form-item label="销售方电话:" prop="sellerTelNum">
          <el-input v-model="formData.sellerTelNum" :clearable="true"  placeholder="请输入销售方电话" />
       </el-form-item>
        <el-form-item label="销售方开户行:" prop="sellerBankName">
          <el-input v-model="formData.sellerBankName" :clearable="true"  placeholder="请输入销售方开户行" />
       </el-form-item>
        <el-form-item label="销售方账号:" prop="sellerBankAccNum">
          <el-input v-model="formData.sellerBankAccNum" :clearable="true"  placeholder="请输入销售方账号" />
       </el-form-item>
        <el-form-item label="购买方税号:" prop="buyerIdNum">
          <el-input v-model="formData.buyerIdNum" :clearable="true"  placeholder="请输入购买方税号" />
       </el-form-item>
        <el-form-item label="购买方名称:" prop="buyerName">
          <el-input v-model="formData.buyerName" :clearable="true"  placeholder="请输入购买方名称" />
       </el-form-item>
        <el-form-item label="购买方地址:" prop="buyerAddr">
          <el-input v-model="formData.buyerAddr" :clearable="true"  placeholder="请输入购买方地址" />
       </el-form-item>
        <el-form-item label="购买方电话:" prop="buyerTelNum">
          <el-input v-model="formData.buyerTelNum" :clearable="true"  placeholder="请输入购买方电话" />
       </el-form-item>
        <el-form-item label="购买方开户行:" prop="buyerBankName">
          <el-input v-model="formData.buyerBankName" :clearable="true"  placeholder="请输入购买方开户行" />
       </el-form-item>
        <el-form-item label="购买方账号:" prop="buyerBankAccNum">
          <el-input v-model="formData.buyerBankAccNum" :clearable="true"  placeholder="请输入购买方账号" />
       </el-form-item>
        <el-form-item label="合计金额:" prop="totalAmWithoutTax">
          <el-input-number v-model="formData.totalAmWithoutTax" :precision="2" :clearable="true"></el-input-number>
       </el-form-item>
        <el-form-item label="合计税额:" prop="totalTaxAm">
          <el-input-number v-model="formData.totalTaxAm" :precision="2" :clearable="true"></el-input-number>
       </el-form-item>
        <el-form-item label="价税合计:" prop="totalTaxIncludedAmount">
          <el-input-number v-model="formData.totalTaxIncludedAmount" :precision="2" :clearable="true"></el-input-number>
       </el-form-item>
        <el-form-item label="价税合计大写:" prop="totalTaxIncludedAmountInChinese">
          <el-input v-model="formData.totalTaxIncludedAmountInChinese" :clearable="true"  placeholder="请输入价税合计大写" />
       </el-form-item>
        <el-form-item label="开票人:" prop="drawer">
          <el-input v-model="formData.drawer" :clearable="true"  placeholder="请输入开票人" />
       </el-form-item>
        <el-form-item label="开票时间:" prop="requestTime">
          <el-date-picker v-model="formData.requestTime" type="date" placeholder="选择日期" :clearable="true"></el-date-picker>
       </el-form-item>
        <el-form-item>
          <el-button :loading="btnLoading" type="primary" @click="save">保存</el-button>
          <el-button type="primary" @click="back">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import {
  createInvoices,
  updateInvoices,
  findInvoices
} from '@/api/fapiao/invoices'

defineOptions({
    name: 'InvoicesForm'
})

// 自动获取字典
import { getDictFunc } from '@/utils/format'
import { useRoute, useRouter } from "vue-router"
import { ElMessage } from 'element-plus'
import { ref, reactive } from 'vue'


const route = useRoute()
const router = useRouter()

// 提交按钮loading
const btnLoading = ref(false)

const type = ref('')
const formData = ref({
            eIid: '',
            invoiceType: '',
            generalOrSpecialVat: '',
            taxpayerType: '',
            sellerIdNum: '',
            sellerName: '',
            sellerAddr: '',
            sellerTelNum: '',
            sellerBankName: '',
            sellerBankAccNum: '',
            buyerIdNum: '',
            buyerName: '',
            buyerAddr: '',
            buyerTelNum: '',
            buyerBankName: '',
            buyerBankAccNum: '',
            totalAmWithoutTax: 0,
            totalTaxAm: 0,
            totalTaxIncludedAmount: 0,
            totalTaxIncludedAmountInChinese: '',
            drawer: '',
            requestTime: new Date(),
        })
// 验证规则
const rule = reactive({
               eIid : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
})

const elFormRef = ref()

// 初始化方法
const init = async () => {
 // 建议通过url传参获取目标数据ID 调用 find方法进行查询数据操作 从而决定本页面是create还是update 以下为id作为url参数示例
    if (route.query.id) {
      const res = await findInvoices({ ID: route.query.id })
      if (res.code === 0) {
        formData.value = res.data
        type.value = 'update'
      }
    } else {
      type.value = 'create'
    }
}

init()
// 保存按钮
const save = async() => {
      btnLoading.value = true
      elFormRef.value?.validate( async (valid) => {
         if (!valid) return btnLoading.value = false
            let res
           switch (type.value) {
             case 'create':
               res = await createInvoices(formData.value)
               break
             case 'update':
               res = await updateInvoices(formData.value)
               break
             default:
               res = await createInvoices(formData.value)
               break
           }
           btnLoading.value = false
           if (res.code === 0) {
             ElMessage({
               type: 'success',
               message: '创建/更改成功'
             })
           }
       })
}

// 返回按钮
const back = () => {
    router.go(-1)
}

</script>

<style>
</style>
